<!--

    Copyright © 2016-2024 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<form [formGroup]="formGroup" style="min-width: 600px;">
  <mat-toolbar fxLayout="row" color="primary">
    <h2 translate>ws-client.connections.properties</h2>
    <span fxFlex></span>
    <button mat-icon-button
            [mat-dialog-close]="null"
            type="button"
            matTooltip="{{ 'action.close' | translate }}"
            matTooltipPosition="above">
      <mat-icon class="material-icons">close</mat-icon>
    </button>
  </mat-toolbar>
  <mat-progress-bar color="warn" mode="indeterminate" *ngIf="isLoading$ | async">
  </mat-progress-bar>
  <div style="height: 4px;" *ngIf="!(isLoading$ | async)"></div>
  <div mat-dialog-content>
    <section fxLayout="column" fxLayoutGap="16px">
      <section class="slider" fxLayout="row" fxLayoutAlign="flex-start center" fxLayoutGap="8px">
        <mat-slide-toggle formControlName="payloadFormatIndicator">
          {{ 'ws-client.last-will.payload-format-indicator' | translate }}
        </mat-slide-toggle>
        <mat-icon class="tb-mat-20" style="color: rgba(0,0,0,0.18)"
                  [matTooltip]="'ws-client.last-will.payload-format-indicator-hint' | translate"
                  matTooltipPosition="right">
          info</mat-icon>
      </section>
      <mat-form-field class="mat-block" subscriptSizing="dynamic">
        <mat-label translate>ws-client.last-will.content-type</mat-label>
        <input matInput formControlName="contentType">
        <mat-icon matSuffix [matTooltip]="'ws-client.last-will.content-type-hint' | translate">
          help
        </mat-icon>
      </mat-form-field>
      <section fxLayout="row" fxLayoutGap="8px">
        <mat-form-field fxFlex subscriptSizing="dynamic">
          <mat-label translate>ws-client.last-will.message-expiry-interval</mat-label>
          <input matInput formControlName="messageExpiryInterval" type="number" min="0" [max]="calcMax('messageExpiryIntervalUnit')">
          <mat-icon matSuffix [matTooltip]="'ws-client.last-will.message-expiry-interval-hint' | translate">
            help
          </mat-icon>
        </mat-form-field>
        <mat-form-field fxFlex subscriptSizing="dynamic">
          <mat-label translate>timeunit.units</mat-label>
          <mat-select formControlName="messageExpiryIntervalUnit">
            <mat-option *ngFor="let unit of timeUnitTypes" [value]="unit">
              {{ timeUnitTypeTranslationMap.get(unit) | translate }}
            </mat-option>
          </mat-select>
        </mat-form-field>
      </section>
      <mat-form-field class="mat-block" subscriptSizing="dynamic">
        <mat-label translate>ws-client.connections.topic-alias</mat-label>
        <input matInput type="number" min="1" formControlName="topicAlias">
        <mat-icon matSuffix [matTooltip]="'ws-client.connections.topic-alias-hint' | translate">
          help
        </mat-icon>
        <mat-error *ngIf="formGroup.get('topicAlias').errors?.topicAliasMaxError">
          {{ 'ws-client.connections.topic-alias-error' | translate }}
        </mat-error>
      </mat-form-field>
      <mat-form-field fxFlex class="mat-block" subscriptSizing="dynamic">
        <mat-label translate>ws-client.last-will.correlation-data</mat-label>
        <input matInput formControlName="correlationData">
        <mat-icon matSuffix [matTooltip]="'ws-client.last-will.correlation-data-hint' | translate">
          help
        </mat-icon>
      </mat-form-field>
      <mat-form-field class="mat-block" subscriptSizing="dynamic">
        <mat-label translate>ws-client.last-will.response-topic</mat-label>
        <input matInput formControlName="responseTopic">
        <mat-icon matSuffix [matTooltip]="'ws-client.last-will.response-topic-hint' | translate">
          help
        </mat-icon>
      </mat-form-field>
      <tb-user-properties [entity]="props?.userProperties" [mqttVersion]="5" formControlName="userProperties"></tb-user-properties>
    </section>
  </div>
  <div mat-dialog-actions fxLayout="row" fxLayoutAlign="end center">
    <button mat-button color="primary"
            type="button"
            [disabled]="(isLoading$ | async)"
            [mat-dialog-close]="null" cdkFocusInitial>
      {{ 'action.cancel' | translate }}
    </button>
    <button mat-raised-button color="primary"
            (click)="onSave()"
            [disabled]="(isLoading$ | async) || formGroup?.invalid || !formGroup?.dirty">
      {{ 'action.save' | translate }}
    </button>
  </div>
</form>
